package com.atguigu.lease.admin.controller;

import com.atguigu.lease.admin.service.FacilityInfoService;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.bean.FacilityInfo;
import com.atguigu.lease.model.enums.ItemTypeEnum;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LOGIN
 * @title FacilityController
 * @create 2025/6/23 21:47
 * @description
 */
@Tag(name="配套信息列表")
@RestController
@RequestMapping("/admin/facility")
public class FacilityController {
    @Autowired
    FacilityInfoService facilityInfoService;
    @Operation(summary = "1、查询配套信息列表")
    @GetMapping("/list")
    public Result<List<FacilityInfo>> list(
            @Parameter(in = ParameterIn.QUERY,description = "配套类型 1-公寓 2-房间")
            @RequestParam(required = false,value = "type")ItemTypeEnum type //接收参数：springmvc消息转换器生效
            ){
        //响应结果转为json响应给前端时，@JsonValue注解标注的属性会作为FacilityInfo对象 type属性值响应
        return Result.ok(facilityInfoService.list(Wrappers
                .lambdaQuery(FacilityInfo.class)
                //type不能为空，mp根据 在枚举类属性上标注的@EnumValue注解获取属性值传参
                //mp查询到表的结果时，自动解析type字段的值，给枚举类使用了@ENumValue注解的属性
                .eq(type!=null, FacilityInfo::getType, type)

        ));
    }

//    /admin/facility/saveOrUpdate
    @Operation(summary = "2、新增和更新配套信息")
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(
            @RequestBody FacilityInfo facilityInfo
    ){
        return Result.ok(facilityInfoService.saveOrUpdate(facilityInfo));
    }

//    /admin/facility/deleteById?id=57
    @Operation(summary = "3、根据id删除配套信息")
    @DeleteMapping("/deleteById")
    public Result deleteById(
            @Parameter(in = ParameterIn.QUERY,required = true,description = "主键值")
            @RequestParam("id") Integer id
    ){
        return Result.ok(facilityInfoService.removeById(id));
    }
}
